import os
import unittest
import ddt
from common.browser import open_browser
from page.page_index import Index
from page.page_login import Login
from common.data_operation import DataOperation

# 准备数据
report = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
file_name = 'data/user_login.xls'
file_path = os.path.join(report, file_name)
user_account = DataOperation(path=file_path, sheet_name='fail').get_data_todict()


@ddt.ddt
class TestUserLogin(unittest.TestCase):
    # 打开浏览器
    @classmethod
    def setUpClass(cls) -> None:
        driver = open_browser()
        cls.index = Index(driver)
        cls.login = Login(driver)

    @ddt.data(*user_account)
    def test_method(self, data):
        # 请求网址
        self.index.get(self.index.index_url)
        self.login.implicitly_wait()
        # 点击请登录
        self.index.btn_index_login()

        # 登录操作
        self.login.ipt_username(data['username'])
        self.login.ipt_password(data['password'])
        self.login.remember()
        self.login.btn_login()
        # 断言登录失败
        username = self.index.username_text()
        self.assertIsNone(username)

    # 退出登录
    def tearDown(self) -> None:
        self.index.btn_quit_login()

    # 关闭浏览器
    @classmethod
    def tearDownClass(cls) -> None:
        cls.index.quit()


if __name__ == '__main__':
    unittest.main(verbosity=2)



